//
//  KeyBoardTopBar.m
//  RenjiBank
//
//  Created by guopanpan on 13-6-26.
//  Copyright (c) 2013年 lijing. All rights reserved.
//

#import "KeyBoardTopBar.h"

@implementation KeyBoardTopBar
@synthesize view;

- (id)init
{
    self = [super init];
    if (self) {
        prevButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"上一项"
                                                          style:UIBarButtonItemStyleBordered
                                                         target:self
                                                         action:@selector(showPrevious)];
        nextButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一项"
                                                          style:UIBarButtonItemStyleBordered
                                                         target:self
                                                         action:@selector(showNext)];
        hiddenButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"隐藏键盘"
                                                            style:UIBarButtonItemStyleBordered
                                                           target:self
                                                           action:@selector(HiddenKeyBoard)];
        spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                        target:nil
                                                                        action:nil];
        view = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 480, 320, 44)];
        view.barStyle = UIBarStyleBlackTranslucent;
        view.items = [NSArray arrayWithObjects:prevButtonItem,nextButtonItem, spaceButtonItem, hiddenButtonItem, nil];
        allowShowPreAndNext = YES;
        textFields = nil;
        isInNavigationController = YES;
        currentTextField = nil;
    }
    return self;
}

- (void)setIsInNavigationController:(BOOL)isbool
{
    isInNavigationController = isbool;
}

- (void)showPrevious
{
    if (textFields == nil) {
        return;
    }
    
    NSInteger num = -1;
    for (NSInteger i = 0; i < [textFields count]; i++) {
        if ([textFields objectAtIndex:i] == currentTextField) {
            num = i;
            break;
        }
    }
    
    if (num > 0) {
        [[textFields objectAtIndex:num] resignFirstResponder];
        [[textFields objectAtIndex:num - 1] becomeFirstResponder];
        [self showBar:[textFields objectAtIndex:num - 1]];
    }
}

- (void)showNext
{
    if (textFields == nil) {
        return;
    }
    
    NSInteger num = -1;
    for (NSInteger i = 0; i < [textFields count]; i++) {
        if ([textFields objectAtIndex:i] == currentTextField) {
            num = i;
            break;
        }
    }
    
    if (num < [textFields count] - 1) {
        [[textFields objectAtIndex:num] resignFirstResponder];
        [[textFields objectAtIndex:num + 1] becomeFirstResponder];
        [self showBar:[textFields objectAtIndex:num + 1]];
    }
}

- (void)showBar:(UITextField *)textField
{
    currentTextField = textField;
    if (allowShowPreAndNext) {
        [view setItems:[NSArray arrayWithObjects:prevButtonItem, nextButtonItem, spaceButtonItem, hiddenButtonItem, nil]];
    } else {
        [view setItems:[NSArray arrayWithObjects:spaceButtonItem, hiddenButtonItem, nil]];
    }
    
    if (textFields == nil) {
        prevButtonItem.enabled = NO;
        nextButtonItem.enabled = NO;
    } else {
        NSInteger num = -1;
        for (NSInteger i = 0; i < [textFields count]; i++) {
            if ([textFields objectAtIndex:i] == currentTextField) {
                num = i;
                break;
            }
        }
        
        if (num > 0) {
            prevButtonItem.enabled = YES;
        } else {
            prevButtonItem.enabled = NO;
        }
        
        if (num < [textFields count] - 1) {
            nextButtonItem.enabled = YES;
        } else {
            nextButtonItem.enabled = NO;
        }
    }
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    if (isInNavigationController) {
        view.frame = CGRectMake(0, 201 - 40, 320, 44);
    } else {
        view.frame = CGRectMake(0, 201, 320, 44);
    }
    
    [UIView commitAnimations];
}

- (void)setTextFieldsArray:(NSArray *)array
{
    textFields = array;
}

- (void)setAllowShowPreAndNext:(BOOL)isShow
{
    allowShowPreAndNext = isShow;
}

- (void)HiddenKeyBoard
{
    if (currentTextField != nil) {
        [currentTextField resignFirstResponder];
    }
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    view.frame = CGRectMake(0, 480, 320, 44);
    [UIView commitAnimations];
}

- (void)dealloc
{
    [view release];
    [textFields release];
    [prevButtonItem release];
    [nextButtonItem release];
    [spaceButtonItem release];
    [hiddenButtonItem release];
    [currentTextField release];
    
    [super dealloc];
}

@end
